Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

Showing posts with label MathWorks MATLAB. Show all posts
Showing posts with label MathWorks MATLAB. Show all posts

Basic Control System Development Approach for Electro-mechanical System

This article attempts to present a general approach in designing and developing a control system for any type of electro-mechanical system. The design of any robust controller for the electro-mechanical system (for example, an airship system having payload, positioner, and antenna) should involve the following steps.

a) Identification of the governing dynamics for the system

The very first step should identify the parts/elements of the system that play meaningful roles in the overall dynamics of the system. For our airship model, there are four rigid bodies in the system: airship, payload, positioner, and antenna. They are connected in a manner to form a total dynamical system. Before moving forward with the controller, we need to figure out the appropriate dynamical equations to represent the system that needs to be controlled by a controller. To do so, we may consider the following approach.

We may attach body fixed frame to each of the rigid body of the model. The differential equation for each rigid body is derived with respect to the body fixed reference frame. We may consider the ground as an absolute or inertial frame.

Once the differential equations for the rigid bodies are done, next we do the kinematic analyses that will be used for the transformation of the differential equations from the local or body fixed frame to the global or inertial frame for analyses.

We also need to identify the forces acting on the overall system for the governing equations. In practice, there are inertial forces, gravitational forces, buoyancy forces, aerodynamic forces, and so forth that act on the airship system. We may model these forces and incorporate in the differential equations.

Based on all the above assumptions, we may end up with an 8 degree-of-freedom (DOF) system where the airship has 6 DOF, and the positioner has 2 DOF. For the controller, we need to consider these degrees of freedom as a design requirement. 

b) Stability analysis of the system

We may need to linearize the system differential equations before heading towards the controller design. After the linearization, we need to check the stability of the system. Linearization may be necessary since the difficulty may arise in the controller design with complex nonlinear differential equations, which may prevent us to achieve the desired objectives. We may use the basic eigenvalue analysis to determine if the system is stable. At this stage, the linearization of the nonlinear airship model will be finalized for the design of the controller.

c) Controller design in MATLAB SIMULINK

We may choose two approaches to design a controller for the airship system. The first approach is open loop controller that is simple in design and easy to implement. The next approach may be a feedback control system that would be comprehensive and accurate in terms of system responses.

Open loop controller

In this open loop control system design step, we may implement the following SIMULINK model, which shows an actuator block and a plant or the airship system block. We are required to control the position and altitude of the motors that are connected to the antenna and positioner, respectively. 

Open loop control system


















The open loop system would be simple. The following schematic may represent the implementation method. Here, our task is to control the actuator so that it can guide the antenna. There are two actuators involved: one for 2 DOF positioner, and another for antenna. The required inputs are the desired position/altitude of the antenna that may be fed and controlled through the motor drivers. In this open loop condition, there is limited scope to optimize the system response based on the feedback errors.

Implementation of open loop controller
















Closed loop controller

We may also design a (Proportional, Integral , and Derivative) PID controller to control the position and altitude of the actuators. The following SIMULINK diagram shows a simple concept for this task:

Closed loop control system

















In this closed loop feedback control system, we may design and implement a PID controller that will control the actuators for the airship model. The input to the controller may be wind disturbance, position, altitude etc. that need to taken care of precise controlling. Since the goal of this project is to position the antenna, which is also connected to a 2 DOF positioner; we may fit a PID controller in between the actuators taking considerations all the input and output parameters for the motors.

Summary

I would recommend designing the overall airship system in SolidWorks as if it represents a real physical system. Then, the computer-aided-design (CAD) model may be translated into the MATLAB environment, keeping all the physical properties same from SolidWorks. After that, in MATLAB, we may have a lot of options to design a control system conveniently, but the added benefit here is the accurate physical CAD model that needs to be controlled eventually. In SolidWorks, we may run kinematic or dynamic motion analyses to ensure the movement of each rigid part even before the development of a controller.



#ControlSystemDesign   #MATLAB  #SIMULINK  #SolidWorks  #ElectroMechanicalSystem  #ModellingSimulation  #FeedbackControl  #MotionSimulation

A Fixed Free Cantilever Beam Deflection and Stress Analysis with Matlab

This tutorial is related to analyzing the deflection and stress distribution of a cantilever beam. A fixed-free cantilever beam, (Young’s Modulus 𝐸, 𝑏×ℎ cross-section, length 𝐿) is supported at its left-hand end. A Force 𝐹 is applied at a distance 𝑎 from the left-hand end. We will first calculate the shape of the deflected beam and plot the result of deflection, shear and bending moment diagram. Next, we will generate a 2D contour map of the stress distribution along the beam.

A fixed-free cantilever beam

Length of the beam, L = 1000 mm

Width of the beam, b = 50 mm

Height of the beam, h = 155 mm

Distance of the force location, a = 800 mm

Modulus of elasticity, E = 200 Gpa

Applied force, F = 25 KN

The shape of the deflected beam is calculated as [2-4],













The moment of inertia is calculated as,

I = 1/12 bh^3



Cantilever beam deflection
















Cantilever beam deflection
















The following Matlab codes are used to calculate the beam deflection and plot the results.

close all
clear 
clc
 
% Parameters from the question
E = 200*10^9;
a = 0.8;
b = 0.05;
h = 0.155;
I = (1/12)*(b*h^3);
f = 25000;
 
x = 0:0.01:0.8;
delta_1 = - (f*x.^2)./(6.*E.*I).*(3.*a - x);  % for 0 <= x <= a
delta_2 = - (f*a.^2)./(6.*E.*I).*(3.*x - a);  % for a <= x <= L
 
figure(1)
plot(x, delta_1)
xlabel('Length (m)')
ylabel('Beam Deflection (m)')
title('Cantilever Beam Deflection for 0 <= x <= a')
 
figure(2)
plot(x, delta_2)
xlabel('Length (m)')
ylabel('Beam Deflection (m)')
title('Cantilever Beam Deflection for a <= x <= L')


The shear force and bending moment diagrams for the fixed free cantilever beam is shown below:

Shear force and bending moment diagram















The Matlab codes for the calculation of shear and bending moments as well as plotting these diagrams are provided below.


MATLAB Codes

close all
clear
clc
 
L = 1; % Unit in meters
n = 2; % Number of point loads on the beam
 
for i = 1:n
fprintf('Enter the point load and distance where load acts for %d node\n',i)
Wc(i)=input('Enter the load in Newton\n');
Lc(i) = input('Enter the distance of the point load from the fixed end\n');
            
if Lc(i)>L || Lc(i)<0                
    error('Please check the Length')            
end
end
 
NL = zeros(1,n);        
NW = zeros(1,n);
       
for i=1:n
    [a,b]=max(Lc);            
    NL(i)=a;           
    NW(i)=Wc(b);          
    Lc(b)=[];           
    Wc(b)=[];        
end
 
NL(n+1) = 0;
        
% Shear force diagram
figure(1)
Ra = sum(NW);
if NL(1)==L
X1 = L;
F1 = NW(1);
elseif i==1
X1 = [L NL(1) NL(1) 0];
F1 = [0 0 Ra Ra];
else
X1 = [L NL(1)];
F1 = [0 0];
end
S=[]; X=[]; F=[];
        if n>=2
            for i=2:n+1
                x = [NL(i-1) NL(i)];
                S= [S NW(i-1)];
                f = sum(S);
                X= [X x];
                F = [F f f];
            end
        end
subplot(2,1,1)
        
plot([X1 X],[F1 F],'b')
xlim([0 L]);
hline = refline(0,0);
hline.Color = 'k';
legend('Shear Force','Reference')          
title('Shear Force Diagram of Cantilever Beam')
xlabel('Length of the Beam in Meter')
ylabel('Shear Force in Newton')
  
hold on
grid on
fprintf('Reaction Force =%d N\n',Ra)
    
% Bending moment diagram
subplot(2,1,2)
if NL(1)<L
X1=NL(1): L;
M1 = zeros(size(X1));
plot(X1,M1,'r')
hold on 
grid on
end
 
Xm=[]; 
Mm=[];
    
for i=1:n
X = NL(i+1):0.1:NL(i);
M = -(NW(1,1:i)*((NL(1,1:i))'-newX(X,i))) ;
Xm =[X Xm];
Mm =[M Mm];
end    
    
plot(Xm,Mm,'r'
xlim([0 L]);
hline = refline(0,0);
hline.Color = 'k';
legend('Bending Moment','Reference')  
    
title('Bending Moment Diagram of Cantilever Beam')
xlabel('Length of the Beam in Meter')
ylabel('Bending Moment in Newton-Meter')
hold off
grid on
 
function x = newX(X,i)                                                
[~,d] = size(X);                                                 
x = zeros(i,d);                                                
for j=1:i                                                        
x(j,1:d) = X;                                                   
end                                                             
end    

The bending stress in the beam is calculated as [2-4],





Where, δ is calculated as,








In the following, the stress distributions for the cantilever beam are shown. The Matlab codes are appended after the results.

Mesh plot for cantilever beam deflection
















Contour plot for cantilever beam deflection
















Mesh plot for cantilever beam deflection
















Contour plot for cantilever beam deflection
















MATLAB CODES

close all
clear 
clc
 
% Parameters from the question
L = 1;
E = 200*10^9;
a = 0.8;
b = 0.05;
h = 0.155;
I = (1/12)*(b*h^3);
f = 25000;
M1 = f*a;
M2 = f*(L-a);
 
x = 0:0.01:0.8;
y = 0:0.01:0.8;
 
delta_1 = - (f*x.^2)./(6.*E.*I).*(3.*a - x);  % for 0 <= x <= a
delta_2 = - (f*a.^2)./(6.*E.*I).*(3.*x - a);  % for a <= x <= L
 
sigma_1 = (M1.*delta_1)./I;
 
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = (M1.*((f*x.^2)./(6.*E.*I).*(3.*a - x)))./I;
z1 = (M2.*((f*a.^2)./(6.*E.*I).*(3.*x - a)))./I;
 
F = TriScatteredInterp(x,y,z);
ti = -1:.25:1;
[qx,qy] = meshgrid(ti,ti);
qz = F(qx,qy);
 
figure(1)
scatter3(x,y,z)
title('Mesh Plot for 0 <= x <= a')
hold on
mesh(qx,qy,qz)
 
figure(2)
contour(qx,qy,qz)
title('Contour Plot for 0 <= x <= a')
 
F = TriScatteredInterp(x,y,z1);
ti = -1:0.25:1;
[qx,qy] = meshgrid(ti,ti);
qz = F(qx,qy);
 
figure(3)
scatter3(x,y,z)
title('Mesh Plot for a <= x <= L')
hold on
mesh(qx,qy,qz)
 
figure(4)
contour(qx,qy,qz)
title('Contour Plot for a <= x <= L')



REFERENCES

[1] MATLAB 2020b Academic Version from MathWorks (https://www.mathworks.com/)

[2] Budynas-Nisbett, "Shigley's Mechanical Engineering Design," 8th Ed.

[3] Gere, James M., "Mechanics of Materials," 6th Ed.

[4] Lindeburg, Michael R., "Mechanical Engineering Reference Manual for the PE Exam," 13th Ed.

[5] Solid Mechanics Part I: An Introduction to Solid Mechanics by ‪Piaras Kelly‬‬



#FixedFreeBeam   #CantileverBeam   #StressDistribution   #ShearForce   #BendingMoment  #Matlab

Mohr's Circle Representation for the Stress Components of a Structure using Matlab

In this tutorial, we are going to solve a fundamental problem in mechanics of materials using the very popular Mohr's circle method. Mohr's circle is a two dimensional (2D) graphical representation of the stress components in a solid material. The following diagram shows a typical 2D element having all the stress components. Here,

𝞼x = Normal stress along x axis
𝞼y = Normal stress along y axis
𝞽xy = Shear stress on the plane
𝞡 = Inclination angle

Showing a 2D element with the stresses.















Now, let's assume the following parameters to represent the stresses by a Mohr's circle with Matlab codes.

𝞼x = Normal stress along x axis = 115 Mpa
𝞼y = Normal stress along y axis = - 50 Mpa
𝞽xy = Shear stress on the plane = 25 Mpa
𝞡 = Inclination angle = 25 Degree

With the above information at hand, we can draw a Mohr's circle to represent those stresses. The following figure shows the Mohr's circle, which is plotted by Matlab codes appended at the end of this blog post.

Showing Mohr's circle for the stress components














Showing stress versus cut plane angles














We can calculate the transformed normal and shear stresses, which  may be expressed as [2, 3],















The maximum principal stress is defined as [2, 3],











The following figure illustrates the maximum normal and shear stresses, along with other nomenclatures on a typical Mohr’s circle

Showing maximum normal and shear stresses along with other nomenclatures on the Mohr’s circle


MATLAB CODES

close all
clear
clc
 
% Parameters from the question
sigma_x = 115;
sigma_y = -50;
tau_xy = 25;
gridsize = 1000;
% Calling the function "mohrs_circle" defined in another script
[sigma_mohr,tau_mohr,sigma_1,sigma_2,tau_1,tau_2,center_circle,phi] = mohrs_circle(sigma_x,sigma_y,tau_xy,gridsize);
%%
% Plotting the figures
figure;
plot(sigma_mohr,tau_mohr);
grid on;
axis equal;
xlabel('Normal Stess, MPa');
ylabel('Shear Stress, MPa');
title('Mohr Circle for 2D Stresses');
hold on;
plot(sigma_1,0,'r*',sigma_2,0,'r*',center_circle,tau_1,'ro',center_circle,tau_2,'ro',center_circle,0,'r^');
%%
figure;
plot(phi*180/pi,sigma_mohr,'b',phi*180/pi,tau_mohr,'g');
grid on;
xlabel('Cut plane angle (deg)');
ylabel('Stress, MPA');
legend('Normal Stress','Shear Stress')
title('Mohr 2D Circle');

-----------------------------------------------------------------------------------------------------------------------------
function [sigma_mohr,tau_mohr,sigma_1,sigma_2,tau_1,tau_2,center_circle,phi]=mohrs_circle(sigma_x,sigma_y,tau_xy,gridsize)
% This function implements the calculation of the principal stresses
 
phi = linspace(0,pi,gridsize); % Defining the range of the angles for plotting
% Applying the formula or expression based on the stress theory
sigma_mohr = (sigma_x+sigma_y)/2+(sigma_x-sigma_y)/2*cos(2*phi)+tau_xy*sin(2*phi);
tau_mohr = -(sigma_x-sigma_y)/2*sin(2*phi)+tau_xy*cos(2*phi);
sigma_1 = (sigma_x+sigma_y)/2-sqrt(((sigma_x-sigma_y)/2)^2+tau_xy^2);
sigma_2 = (sigma_x+sigma_y)/2+sqrt(((sigma_x-sigma_y)/2)^2+tau_xy^2);
tau_1=sqrt(((sigma_x-sigma_y)/2)^2+tau_xy^2);
tau_2 = -tau_1;
center_circle = (sigma_x+sigma_y)/2;
%phi_p=atan(2*tau_xy/(sigma_x-sigma_y))/2;
end


REFERENCES

[1] Saul K. Fenster, Ansel C. Ugural. Advanced Strength and Applied Elasticity, Fourth Edition. Published by Pearson, 2003

[2] Solid Mechanics Part I: An Introduction to Solid Mechanics by ‪Piaras Kelly‬‬



#Matlab   #SolidMechanics   #MechanicsofMaterials #MohrsCircle

App Development with the Matlab App Designer - Part 1: A Simple Mechanics Calculator

In this tutorial, I am going to walk you through the basics of the app development with the relatively new Matlab App Designer platform. Mathworks has introduced the App designer in the 2016 Matlab edition, which helps you to build your own apps with the default graphical user interface (GUI) and programming environment. I have come to know this handy tool lately, and I am very eager to share my knowledge with you, especially if you are interested in object-oriented programming

Let's see, first how we can access the App Designer option. Open the Matlab and click on the "APPS" from the top menus.

Matlab > APPS > Design App

Matlab App Designer





Next, after clicking the "Design App" icon, the following window will appear. As you can see, there are more options to choose. But, to begin with, we should select a "Blank App".

Selecting a blank app in Matlab App Designer

















Once, you select the "Blank App", you will see the following window. On the left hand, you have the default 'drag and drop' items for your app that you can simply select and bring it to the middle window "Design View", where the development works are carried out. On your right, you have the options to customize the default app items of the left side. For example, if you want to increase the font size of your display, you can do it from the right side menu bar.

A Blank Matlab App Designer Platform

















Now, if you click on the "Code View", you will see default codes already done by Matlab for the basic interface. You can edit codes here or enter your own codes that you would like to incorporate with the simulator.

Matlab App Designer Platform Code View


















Now, let's try a very simple example, let's make the very first app by the Matlab App Designer. We will make a calculator for basic calculation in mechanics where it will calculate the force based on the two input data: mass and acceleration. As we know from Newton's second law of motion that the force is a product of the mass and acceleration, this formula is simply implemented in the development of our very first app or simulator by Matlab. Let's follow the following screenshots closely.

Selecting objects in App Designer to build an app


In the above screenshot, as you can see, from the left side menu bar, objects are dragged and placed in the middle window or the design view section. To make a calculator with two inputs, I chose a simple display for the mass input and for the acceleration, I selected a knob, which can be rotated to fix a value. To show the output that is "Force", I dragged the text objects similarly from the left side. You can also format the style of the texts, sizes, etc. from the right side menu bar.

Now, it's time to code the objects that we just placed in the design window a while ago. This is the part that we may call object-oriented programming. So, what are the objects that have been selected here - an execution button, text displays, and a knob for the acceleration parameter. If we press the execution button after providing the inputs, we should have the output displayed on the right side. This is the goal for our first app development.

How to format app objects in the app environment

















We will now add codes to the "Execute" button for our goal achievement. In the following image, we see that how we can access the "Callbacks", which will eventually take us to the code environment that is shown in the next screenshot.

How to access the function Callbacks

















We need to add the formula "Force = Mass × Acceleration". To do so, as you see below, we need to copy the values of the functions under each object (e.g., "Object Mass (kg)", "Choose Acceleration", and "Force (N)") that are selected and write or organize them as per the given formula. For example, the output display is the "Force (N)" and its function value is "app.ForceNEditField.Value", which is the left side of the equation. Then, the right side is the mass times acceleration that is represented by the "app.ObjectMasskgEditField.Value" and "app.ChooseAccelerationKnob.Value" subsequently under the respective functions' values.

Coding platform for the Matlab App Designer

















So, that is all! We have achieved our goal - made a very simple app that calculates the force given the mass and acceleration inputs.

A simple app for mechanics calculation

























#Matlab   #MatlabAppDesigner   #MechanicsCalculator  #ObjectOrientedProgramming